package org.dromara.third.convert.riskEvent.web;

import com.alibaba.fastjson.JSONObject;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.exception.BusinessServiceException;
import org.dromara.common.core.utils.DateUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.core.utils.ValidateUtil;
import org.dromara.system.domain.RiskEvent;
import org.dromara.system.mapper.RiskEventMapper;
import org.dromara.third.auth.beans.constans.ExceptionEnum;
import org.dromara.third.auth.common.auth.TokenThreadLocal;
import org.dromara.third.auth.util.AESUtil;
import org.dromara.third.convert.riskEvent.dto.RiskEventDTO;
import org.dromara.third.convert.riskEvent.validator.RiskEventValidator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RequiredArgsConstructor
@RestController
@Slf4j
@RequestMapping("/api/v1")
public class ConvertRiskEventController {

    @Autowired
    private RiskEventMapper riskEventMapper;

    @PostMapping("/riskEvent")
    @Transactional
    public R<Void> addRiskEvent(@RequestBody String jsonReq) {
        String orgCode = TokenThreadLocal.getThreadLocal();
        RiskEventDTO riskEventDTO;
        try {
            String decrypt = AESUtil.decrypt(jsonReq);
            riskEventDTO = JSONObject.parseObject(decrypt, RiskEventDTO.class);
        } catch (Exception e) {
            log.info("解析参数异常");
            throw new BusinessServiceException(ExceptionEnum.AuthException06.getCode(), ExceptionEnum.AuthException06.getMessage());
        }
        // 校验参数
        List<String> errorMessageList = new ArrayList<>();
        ValidateUtil.validate(errorMessageList, riskEventDTO, new RiskEventValidator());
        if (!CollectionUtils.isEmpty(errorMessageList)) {
            String errorMessage = StringUtils.join(errorMessageList, ",");
            throw new BusinessServiceException(ExceptionEnum.AuthException06.getCode(), errorMessage);
        }
        RiskEvent riskEvent = assembleRiskEventData(orgCode, riskEventDTO);
        int count = riskEventMapper.countWithOutDelFlag(riskEvent.getId());
        if (count == 0) {
            riskEventMapper.insert(riskEvent);
        } else {
            riskEventMapper.updateWithOutDelFlag(riskEvent);
        }
        return R.ok();
    }

    private RiskEvent assembleRiskEventData(String orgCode, RiskEventDTO dto) {
        RiskEvent riskEvent = new RiskEvent();
        try {
            riskEvent.setId(dto.getId());
            riskEvent.setRiskEventName(dto.getRiskEventName());
            riskEvent.setRiskUnitId(dto.getRiskUnitId());
            riskEvent.setDelFlag(Long.parseLong(dto.getDelFlag()));
            riskEvent.setCreateDept(Long.parseLong(dto.getCreateDept()));
            riskEvent.setCreateBy(Long.parseLong(dto.getCreateBy()));
            riskEvent.setCreateTime(DateUtils.parseDate(dto.getCreateTime()));
            riskEvent.setUpdateBy(Long.parseLong(dto.getUpdateBy()));
            riskEvent.setUpdateTime(DateUtils.parseDate(dto.getUpdateTime()));
            riskEvent.setTenantId(orgCode);
        } catch (Exception e) {
            throw new BusinessServiceException(ExceptionEnum.AuthException07.getCode(), "组装风险事件信息异常");
        }
        return riskEvent;
    }
}
